﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iLangl.Model
{
	public class CompanyResponseContext : BaseResponseContext
	{
		ICompany company = null;

		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (document == null)
				return;

			XmlNode nodecompany = document.SelectSingleNode("//Company");

			if (nodecompany == null)
				return;

			company = Facade.Instance.ModelFactory.CreateCompany();

			ParseExtendedInfo(nodecompany, company);

			if(nodecompany.Attributes["id"] != null)
			{
				company.Id = nodecompany.Attributes["id"].Value;
			}

			if (nodecompany.Attributes["name"] != null)
			{
				company.Name = nodecompany.Attributes["name"].Value;
			}

			if (nodecompany.Attributes["address"] != null)
			{
				company.Address = nodecompany.Attributes["address"].Value;
			}

			if (nodecompany.Attributes["city"] != null)
			{
				company.City = nodecompany.Attributes["city"].Value;
			}

			if (nodecompany.Attributes["state"] != null)
			{
				company.State = nodecompany.Attributes["state"].Value;
			}

			if (nodecompany.Attributes["zip"] != null)
			{
				company.Zip	 = nodecompany.Attributes["zip"].Value;
			}

			if (nodecompany.Attributes["country"] != null)
			{
				company.Country = nodecompany.Attributes["country"].Value;
			}

			if (nodecompany.Attributes["phone"] != null)
			{
				company.Phone = nodecompany.Attributes["phone"].Value;
			}

			if (nodecompany.Attributes["currency"] != null)
			{
				company.Currency = nodecompany.Attributes["currency"].Value;
			}

			//Extract Billing Info
			BillingInfoResponseContext responsebillingInfocontext = new BillingInfoResponseContext();
			responsebillingInfocontext.SetContext(nodecompany);
			company.BillingInfo = (BillingInfo)responsebillingInfocontext.GetContext();

			//Extract Contacts
			ContactResponseContext responsecontactcontext = new ContactResponseContext();
			responsecontactcontext.SetContext(nodecompany);

			object result =responsecontactcontext.GetContext();
			if (result is Contact[])
				company.Contacts = (Contact[])result;

			if (result is Contact)
				company.AddContact((Contact)result);

			
		}


		public override object GetContext()
		{
			return company;
		}


	}
}
